package com.hqyj.shop.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.hqyj.shop.pojo.Manager;
import com.hqyj.shop.pojo.User;
import com.hqyj.shop.service.userService;
import com.hqyj.shop.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private userService userService;

    @RequestMapping("/login")
    public void login(HttpServletRequest arg0, HttpServletResponse arg1) throws IOException {
        //1.使用工具类话验证码 width, height, codeCount, circleCount依次对应验证码的宽度  高度  验证码的个数  干扰个数
        CircleCaptcha cc = CaptchaUtil.createCircleCaptcha(110, 40, 4, 8);

        //得到正确的验证码，并将数据保存到session
        String rightCode = cc.getCode();
        //System.out.println(rightCode);
        arg0.getSession().setAttribute("rightCode", rightCode);
        //2.得到一个绑定浏览器的输出流  跟HttpServletResponse有关系
        ServletOutputStream sos = arg1.getOutputStream();
        //3.将验证码写给浏览器
        cc.write(sos);
        //4.刷新并关闭流
        sos.flush();
        sos.close();
    }

    @RequestMapping("/zuChe")
    public void zuChe(User user,HttpServletResponse response) {
        System.out.println(user);
        String username1 = user.getUserName();
        String password1 = user.getUserPass();
        String password = password1;
        String salt = username1;
        String hashedPass = MD5Util.md5Hash(password, salt);
        user.setUserPass(hashedPass);

        boolean b = userService.registerUser(user);
        if(b){
            try {
                response.getWriter().write("ok");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }



    //管理员登录
    @RequestMapping("/loginManager")
    public void loginManager(String username, String password, String yzm, ModelMap modelMap, HttpServletResponse response, HttpServletRequest request) throws IOException {
        //检查请求参数
        System.out.println(yzm);
        if (yzm != null && yzm!="") {
            String rightCode = (String) request.getSession().getAttribute("rightCode");
            System.out.println(rightCode.equalsIgnoreCase(yzm));
            if (rightCode.equalsIgnoreCase(yzm)) {

                Manager manager = userService.queryManagerByUsernameAndPassword(username, password);

                if (manager == null) {
                    try {
                        response.getWriter().write("error");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    request.getSession().setAttribute("User", manager);
                    try {
                        response.getWriter().write("success");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }


            } else {
                try {
                    response.getWriter().write("ok");
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }


        }else {
            response.getWriter().write("1");
        }



    }

    @RequestMapping("/forgetPassword")
    public void forgetPassword(String userName, String phone, String password, HttpServletResponse response) {
        System.out.println(userName);
        System.out.println(phone);



        User user = userService.queryUserByFor(userName, phone);
        System.out.println(user);
        if (user != null) {
            if (user.getPhone().equals(phone) && user.getUserName().equals(userName)) {

                String username1 = user.getUserName();
                String password1 = password;

                String salt = username1;
                String hashedPass = MD5Util.md5Hash(password1, salt);



                userService.updatePassword(userName, phone, hashedPass);
                try {
                    response.getWriter().write("ok");
                } catch (IOException e) {
                    e.printStackTrace();
                }

            } else {
                try {
                    response.getWriter().write("okkk");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


        } else {
            try {
                response.getWriter().write("okk");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

    @RequestMapping("/shirologinUser")
    @ResponseBody
    public Map<String, Object> shirologinUser(String username, String password, String yzm, ModelMap modelMap, HttpServletResponse response, HttpServletRequest request) {
        //检查请求参数
        System.out.println("123");
        Map<String, Object> result = new HashMap<>();
      if(yzm!=null && yzm!=""){
          String rightCode = (String) request.getSession().getAttribute("rightCode");
          if(yzm.equalsIgnoreCase(rightCode)){

              result = userService.login1(username, password);

              return result;


          }else {
              result.put("code",1);
              result.put("message","验证码错误");
              return result;
          }
      }else
          result.put("code",0);
          result.put("message","验证码为空");
          return result;
    }
    @RequestMapping("/shirologinManager")
    @ResponseBody
    public Map<String, Object> shirologinManager(String username, String password, String yzm, ModelMap modelMap, HttpServletResponse response, HttpServletRequest request) {
        //检查请求参数

        Map<String, Object> result = new HashMap<>();
        if(yzm!=null && yzm!=""){
            String rightCode = (String) request.getSession().getAttribute("rightCode");
            if(yzm.equalsIgnoreCase(rightCode)){

                result = userService.login1(username, password);
                return result;


            }else {
                result.put("code",1);
                result.put("message","验证码错误");
                return result;
            }
        }else
            result.put("code",0);
        result.put("message","验证码为空");
        return result;



    }
    @RequestMapping("/logout")
    public String logout(){
        //调用服务层实现业务
        Map<String, Object> logout = userService.logout();
        return "redirect:/login.jsp";
    }
}
